
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.document.operation.impl {
import org.goverla.collections.ArrayList;

import org.waveprotocol.wave.model.document.operation.AnnotationBoundaryMap;
import org.waveprotocol.wave.model.document.operation.Attributes;
import org.waveprotocol.wave.model.document.operation.AttributesUpdate;
import org.waveprotocol.wave.model.document.operation.BufferedDocOp;

public class DocOpBuilder {
	//An empty Array of DocOpComponent
	private static  var EMPTY_ARRAY:Array= new Array(); 
	
	private  var accu:ArrayList= new ArrayList();
	
	/**
	 * Constructs an operation from this builder's state.
	 *
	 * Behaviour is undefined if this builder is used after calling this method.
	 */
	public final function build():BufferedDocOp {
		return BufferedDocOpImpl.create(accu);
	}
	
	// This is dangerous; we currently use it for ill-formedness-detection
	// tests, and may use it for efficiency in other places in the future.
	public final function buildUnchecked():BufferedDocOp {
		// TODO: This should not need a call to asInitialization().
		return BufferedDocOpImpl.createUnchecked(accu);
	}
	
	public final function annotationBoundary(map:AnnotationBoundaryMap):DocOpBuilder {
		accu.addItem(new AnnotationBoundary(map));
		return this;
	}
	public final function characters(s:String):DocOpBuilder {
		accu.addItem(new Characters(s));
		return this;
	}
	public final function elementEnd():DocOpBuilder {
		accu.addItem(ElementEnd.INSTANCE);
		return this;
	}
	public final function elementStart(type:String, attrs:Attributes):DocOpBuilder {
		accu.addItem(new ElementStart(type, attrs));
		return this;
	}
	public final function deleteCharacters(s:String):DocOpBuilder {
		accu.addItem(new DeleteCharacters(s));
		return this;
	}
	public final function retain(itemCount:int):DocOpBuilder {
		accu.addItem(new Retain(itemCount));
		return this;
	}
	public final function deleteElementEnd():DocOpBuilder {
		accu.addItem(DeleteElementEnd.INSTANCE);
		return this;
	}
	public final function deleteElementStart(type:String, attrs:Attributes):DocOpBuilder {
		accu.addItem(new DeleteElementStart(type, attrs));
		return this;
	}
	public final function replaceAttributes(oldAttrs:Attributes, newAttrs:Attributes):DocOpBuilder {
		accu.addItem(new ReplaceAttributes(oldAttrs, newAttrs));
		return this;
	}
	public final function updateAttributes(update:AttributesUpdate):DocOpBuilder {
		accu.addItem(new UpdateAttributes(update));
		return this;
	}
}
}